<?php
    namespace Home\Controller;
    use Think\Controller;
    class GongzuoliangshezhiController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");

        $item_code = I("get.item_code");
        $item_name = I("get.item_name");
        $item_class = I("get.item_class");

        $where = "1 = 1 ";
        if(!empty($item_code))
        {
            $where.= " and r.ITEM_CODE like '%$item_code%'";
        }
        if(!empty($item_name))
        {
            $where.= " and r.ITEM_NAME like '%$item_name%'";
        }
        if(!empty($item_class))
        {
            $where.= " and r.ITEM_CLASS like '%$item_class%'";
        }

        // $list = M("hisdata.price_list")->select();

        $data = M("cbhs.rbrvs_distribution_calc_schm r")->limit(($page-1)*$limit,$limit)->where($where)->select();

        // for ($i=0; $i < count($data); $i++) 
        // { 
        //     $data[$i]['exit'] = 1;
        // }
        // for ($i=0; $i < count($list); $i++) 
        // { 
        //     $list[$i]['exit'] = 0;
        // }

        // for ($i=0; $i < count($list); $i++) 
        // { 
        //     $list_info = $list[$i];
        //     for ($j=0; $j < count($data); $j++) 
        //     { 
        //         $data_info = $data[$j];
        //         if($data_info['item_code'] == $list_info['item_code'])
        //         {
        //             unset($list[$i]);
        //         }
        //     }
        // }

        // for ($i=0; $i < count($list); $i++) 
        // { 
        //     $data[] = $list[$i];
        // }

        // $arr = [];
        // for ($i=(($page-1)*$limit); $i < ($page*$limit); $i++) 
        // {
        //     $arr[] = $data[$i];
        // }

        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("cbhs.rbrvs_distribution_calc_schm r")->where($where)->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $item_code = I("post.item_code");

        $info = M("hisdata.price_list")->where("item_code = '$item_code'")->find();
        
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }



    public function save_one()
    {
        $data = I("post.");
        $item_code = $data["item_code"];
        unset($data["item_code"]);

        $result = M("cbhs.rbrvs_distribution_calc_schm")->where("item_code = '$item_code'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $data["dept_date"] = time();
        $result = M("cbhs.rbrvs_distribution_calc_schm")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function get_cbhs_distribution_calc_schm()
    {
        $data = M("cbhs.cbhs_distribution_calc_schm")->limit(($page-1)*$limit,$limit)->select();
        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("cbhs.cbhs_distribution_calc_schm")->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_sys_dept_dict()
    {
        $data = M("comm.sys_dept_dict")->limit(($page-1)*$limit,$limit)->select();
        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("comm.sys_dept_dict")->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function set_field()
    {
        $data_all = I("post.");

        $item_code = $data_all["item_code"];

        unset($data_all["item_code"]);

        $data[$data_all['field']] = $data_all['value'];

        $result = M("cbhs.rbrvs_distribution_calc_dept")->where("ITEM_CODE = '$item_code'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
        $this->ajaxReturn($json,"json");
    }

    public function get_teshu_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");

        $item_code = I("get.item_code");
        $item_name = I("get.item_name");
        $item_class = I("get.item_class");

        $where = "1 = 1 ";
        if(!empty($item_code))
        {
            $where.= " and ITEM_CODE like '%$item_code%'";
        }
        if(!empty($item_name))
        {
            $where.= " and ITEM_NAME like '%$item_name%'";
        }
        if(!empty($item_class))
        {
            $where.= " and CLASS_TYPE like '%$item_class%'";
        }

        $data = M("cbhs.rbrvs_distribution_calc_dept")->where($where)->limit(($page-1)*$limit,$limit)->select();
        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("cbhs.rbrvs_distribution_calc_dept")->where($where)->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_price_list()
    {
        $data = M("hisdata.price_list")->field("item_code")->select();
        
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

}